﻿using System;
using System.Threading;
using System.Windows.Forms;
using ActiveHomeScriptLib;

namespace ActiveScript
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();

            for (int iHouse = 0; iHouse < 16; iHouse++) {
                for (int iUnit = 0; iUnit < 16; iUnit++) {
                    AddressComboBox.Items.Add(Convert.ToString(Convert.ToChar('A' + iHouse)) + Convert.ToString(iUnit + 1));
                }
            }

            SecurityTypeComboBox.Hide();
            SecurityTypeComboBox.Items.Add("Standard");
            SecurityTypeComboBox.Items.Add("Extended");
            SecurityTypeComboBox.SelectedIndex = 1;

            ActionComboBox.SelectedIndex = 0;
            ActionComboBox.Select();

            AddressComboBox.SelectedIndex = 0;
            CommandComboBox.SelectedIndex = 0;

            ActiveHome = new ActiveHomeClass();
            ActiveHome.RecvAction += new _DIActiveHomeEvents_RecvActionEventHandler(OnActiveHomeRecv);

        }

        private void SendButton_Click(object sender, EventArgs e)
        {
            try
            {
                String strData = null;
                String strAction = ActionComboBox.SelectedItem.ToString();
                switch (strAction.ToUpper())
                {
                    case "SENDPLC":
                        {
                            strData = AddressComboBox.Text + " " + CommandComboBox.Text;
                            if (DataTextBox.Text.Length > 0)
                                strData += " " + DataTextBox.Text;

                            object Code = ActiveHome.SendAction(strAction, strData, null, null);
                            StatusTextBox.AppendText(strAction + " " + strData + " (" + Convert.ToUInt32(Code) + ")\r\n");
                            break;
                        }

                    case "SENDRAWPLC":
                        {
                            strData = CommandComboBox.Text;
                            object Code = ActiveHome.SendAction(strAction, strData, null, null);
                            StatusTextBox.AppendText(strAction + " " + strData + " (" + Convert.ToUInt32(Code) + ")\r\n");
                            break;
                        }

                    case "SENDRF":
                        {
                            strData = AddressComboBox.Text + " " + CommandComboBox.Text;
                            if (DataTextBox.Text.Length > 0)
                                strData += " " + DataTextBox.Text;

                            RadioFreqForm rfForm = new RadioFreqForm();
                            if (rfForm.ShowDialog() == DialogResult.OK)
                                strData += " " + rfForm.m_nTransmit + " " + rfForm.m_nDelay;
                            else
                                strData += " 0 0";

                            object Code = ActiveHome.SendAction(strAction, strData, null, null);
                            StatusTextBox.AppendText(strAction + " " + strData + " (" + Convert.ToUInt32(Code) + ")\r\n");
                            break;
                        }

                    case "SENDSECURERF":
                        {
                            SecureForm sCodeForm = new SecureForm();
                            if (sCodeForm.ShowDialog() == DialogResult.OK)
                            {
                                strData = CommandComboBox.Text + " " + SecurityTypeComboBox.Text + " " + sCodeForm.GetSecurityCode();
                                RadioFreqForm rfForm = new RadioFreqForm();
                                if (rfForm.ShowDialog() == DialogResult.OK)
                                    strData += " " + rfForm.m_nTransmit + " " + rfForm.m_nDelay;
                                else
                                    strData += " 0 0";

                                object Code = ActiveHome.SendAction(strAction, strData, null, null);
                                StatusTextBox.AppendText(strAction + " " + strData + " (" + Convert.ToUInt32(Code) + ")\r\n");
                            }

                            break;
                        }

                    case "SENDSECUREHOMECONTROLRF":
                        {
                            SecureForm sCodeForm = new SecureForm();
                            if (sCodeForm.ShowDialog() == DialogResult.OK)
                            {
                                strData = AddressComboBox.Text + " " + CommandComboBox.Text + " " + sCodeForm.GetSecurityCode();
                                RadioFreqForm rfForm = new RadioFreqForm();
                                if (rfForm.ShowDialog() == DialogResult.OK)
                                    strData += " " + rfForm.m_nTransmit + " " + rfForm.m_nDelay;
                                else
                                    strData += " 0 0";

                                object Code = ActiveHome.SendAction(strAction, strData, null, null);
                                StatusTextBox.AppendText(strAction + " " + strData + " (" + Convert.ToUInt32(Code) + ")\r\n");
                            }

                            break;
                        }

                    case "QUERYPLC":
                        {
                            strData = AddressComboBox.Text + " " + CommandComboBox.Text;
                            object Code = ActiveHome.SendAction(strAction, strData, null, null);
                            StatusTextBox.AppendText(strAction + " " + strData + " (" + Convert.ToUInt32(Code) + ")\r\n");
                            break;
                        }
                }
            }
            catch (Exception exception) {
                MessageBox.Show(exception.Message, "SDK Error");
            }
        }

        private void ActionComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (ActionComboBox.SelectedItem.ToString().ToUpper())
            {
                case "SENDPLC":
                {
                    AddressComboBox.Enabled = true;
                    CommandComboBox.Enabled = true;
                    DataTextBox.Enabled = false;
                    SecurityTypeComboBox.Hide();

                    CommandComboBox.Items.Clear();
                    CommandComboBox.Items.Add("On");
                    CommandComboBox.Items.Add("Off");
                    CommandComboBox.Items.Add("Dim");
                    CommandComboBox.Items.Add("Bright");
                    CommandComboBox.Items.Add("AllLightsOn");
                    CommandComboBox.Items.Add("AllLightsOff");
                    CommandComboBox.Items.Add("AllUnitsOff");
                    CommandComboBox.Items.Add("PresetDim1");
                    CommandComboBox.Items.Add("PresetDim2");
                    CommandComboBox.Items.Add("StatusOn");
                    CommandComboBox.Items.Add("StatusOff");
                    CommandComboBox.Items.Add("StatusRequest");
                    CommandComboBox.Items.Add("ExtCode");
                    CommandComboBox.Items.Add("ExtendedDataTransfer");
                    CommandComboBox.Items.Add("HailRequest");
                    CommandComboBox.Items.Add("HailAcknowledge");
                    CommandComboBox.SelectedIndex = 0;

                    break;
                }

                case "SENDRAWPLC":
                {
                    AddressComboBox.Enabled = true;
                    CommandComboBox.Enabled = false;
                    DataTextBox.Enabled = true;
                    SecurityTypeComboBox.Hide();

                    CommandComboBox.Items.Clear();
                    CommandComboBox.Items.Add("06 62");
                    CommandComboBox.SelectedIndex = 0;

                    break;
                }

                case "SENDRF":
                {
                    AddressComboBox.Enabled = true;
                    CommandComboBox.Enabled = true;
                    DataTextBox.Enabled = false;
                    SecurityTypeComboBox.Hide();

                    CommandComboBox.Items.Clear();
                    CommandComboBox.Items.Add("On");
                    CommandComboBox.Items.Add("Off");
                    CommandComboBox.Items.Add("Dim");
                    CommandComboBox.Items.Add("Bright");
                    CommandComboBox.Items.Add("AllOn");
                    CommandComboBox.Items.Add("AllOff");
                    CommandComboBox.Items.Add("AllLightsOn");
                    CommandComboBox.Items.Add("AllLightsOff");
                    CommandComboBox.Items.Add("AllUnitsOff");
                    CommandComboBox.SelectedIndex = 0;

                    break;
                }

                case "SENDSECURERF":
                {
                    AddressComboBox.Enabled = false;
                    CommandComboBox.Enabled = true;
                    DataTextBox.Enabled = false;
                    
                    SecurityTypeComboBox.Show();

                    CommandComboBox.Items.Clear();
                    CommandComboBox.Items.Add("FullArmDelayed");
                    CommandComboBox.Items.Add("FullArmInstant");
                    CommandComboBox.Items.Add("DisarmDelayed");
                    CommandComboBox.Items.Add("DisarmInstant");
                    CommandComboBox.Items.Add("HomeArmDelayed");
                    CommandComboBox.Items.Add("HomeArmInstant");
                    CommandComboBox.Items.Add("HomeDisarmInstant");
                    CommandComboBox.Items.Add("HomeDisarmDelayed");
                    CommandComboBox.Items.Add("HomePanicAlarmInstant");
                    CommandComboBox.Items.Add("HomePanicAlarmDelayed");
                    CommandComboBox.Items.Add("PanicAlarmInstant");
                    CommandComboBox.Items.Add("PanicAlarmDelayed");
                    CommandComboBox.Items.Add("MedicalDisarmDelayed");
                    CommandComboBox.Items.Add("MedicalDisarmInstant");
                    CommandComboBox.Items.Add("MedicalPanicAlarmDelayed");
                    CommandComboBox.Items.Add("MedicalPanicAlarmInstant");
                    CommandComboBox.Items.Add("MedicalAlarm");
                    CommandComboBox.Items.Add("SilentAlarm");
                    CommandComboBox.Items.Add("DelayedLightOn");
                    CommandComboBox.Items.Add("InstantLightOn");
                    CommandComboBox.Items.Add("DelayedLightOff");
                    CommandComboBox.Items.Add("InstantLightOff");
                    CommandComboBox.Items.Add("ToggleLight");
                    CommandComboBox.Items.Add("ArmSilent");
                    CommandComboBox.Items.Add("ToggleDog");
                    CommandComboBox.Items.Add("InstantBurglarAlarm");
                    CommandComboBox.Items.Add("DelayedBurglarAlarm");
                    CommandComboBox.Items.Add("InstantBurglarClosed");
                    CommandComboBox.Items.Add("DelayedBurglarClosed");
                    CommandComboBox.Items.Add("FireAlarm");
                    CommandComboBox.Items.Add("FireOkay");
                    CommandComboBox.Items.Add("PIR");
                    CommandComboBox.Items.Add("PIROkay");
                    CommandComboBox.Items.Add("Dusk");
                    CommandComboBox.Items.Add("TamperInstantBurglarAlarm");
                    CommandComboBox.Items.Add("TamperDelayedBurglarAlarm");
                    CommandComboBox.Items.Add("TamperInstantBurglarClosed");
                    CommandComboBox.Items.Add("TamperDelayedBurglarClosed");
                    CommandComboBox.Items.Add("TamperFireAlarm");
                    CommandComboBox.Items.Add("TamperFireOkay");
                    CommandComboBox.Items.Add("TamperPIR");
                    CommandComboBox.Items.Add("TamperPIROkay");
                    CommandComboBox.Items.Add("TamperDusk");
                    CommandComboBox.SelectedIndex = 0;

                    break;
                }

                case "SENDSECUREHOMECONTROLRF":
                {
                    AddressComboBox.Enabled = true;
                    CommandComboBox.Enabled = true;
                    DataTextBox.Enabled = false;
                    SecurityTypeComboBox.Hide();

                    CommandComboBox.Items.Clear();
                    CommandComboBox.Items.Add("On");
                    CommandComboBox.Items.Add("Off");
                    CommandComboBox.Items.Add("Dim");
                    CommandComboBox.Items.Add("Dusk");
                    CommandComboBox.Items.Add("Bright");
                    CommandComboBox.Items.Add("AllLightsOn");
                    CommandComboBox.Items.Add("AllLightsOff");
                    CommandComboBox.Items.Add("AllUnitsOff");
                    CommandComboBox.SelectedIndex = 0;

                    break;
                }

                case "QUERYPLC":
                {
                    AddressComboBox.Enabled = true;
                    DataTextBox.Enabled = false;

                    CommandComboBox.Items.Clear();
                    CommandComboBox.Items.Add("On");
                    CommandComboBox.Items.Add("Dim");
                    CommandComboBox.SelectedIndex = 0;

                    break;
                }
            }
        }

        private void OnActiveHomeRecv(object strAction, object Address, object Command, object Reserved1, object Reserved2, object Reserved3, object Reserved4)
        {
            String strMsg = "";

            if (((String)strAction).ToUpper() == "RECVPLC")
                strMsg += "Recieved Powerline Command:";

            strMsg += " " + Address.ToString().ToUpper();
            strMsg += " " + Command.ToString().ToUpper();

            if (Reserved1.ToString().Length > 0)
                strMsg += " " + Reserved1.ToString().ToUpper();

            if (Reserved2.ToString().Length > 0)
                strMsg += " " + Reserved2.ToString().ToUpper();

            if (Reserved3.ToString().Length > 0)
                strMsg += " " + Reserved3.ToString().ToUpper();

            if (Reserved3 != null && Reserved3.ToString().Length > 0)
                strMsg += " " + Reserved3.ToString().ToUpper();

            StatusTextBox.AppendText(strMsg + "\r\n");
        }
    }
}